#include "Julianna.h"
#include "activities.h"

#include "Juli_inputs_line.h"
#include "Juli_keyboards.h"
#include <string.h>

void activity_one_init()
{
	// interface
	// 412:+32 text show

	public_a_ui_compent_id(4, 412 + 32 + 4, 360 - 4, 32, "slider", volume_pannel_on_pressed, show_volume_pannel);
	// same y, width 42, height:32, puts two buttons
	// delete
	public_a_ui_compent_id(8 + 360, 412 + 32 + 4, 42, 32, "X", button_on_pressed, show_button);
	// send
	public_a_ui_compent_id(8 + 360 + 42, 412 + 32 + 4, 42, 32, "S", button_on_pressed, show_button);
	// push a keyboard
	//keyboard_layout();
	keyboard_layout_setup();
	inputs_init();
	int rtc = public_a_fun_link_int("reload", reload_activity, 0);
	if (rtc < 0)
	{
		lily_out("error init -0x347");
	}

	// kb_interface_control_type = inputs_control_event;
	// kb_interface_getc = inputs_key_typed;
}

int slidder_now = (360 - 4) * 0.27f;
//show num pannel with just plain text
// x:62, y:412, w:360-4, h:16
void show_volume_pannel(JUI this, ViewEvent e)
{
	if (e == OnCreate)
		Juli_draw_round_rect(-2, -2, this->width + 4, this->height + 4, 15);
	//Juli_fill_round_rect(0, 0, this->width-4 , this->height, 14, Color_white);

	//Juli_fill_circle(8, this->height / 2, 15, Color_lightblue);
	Juli_fill_round_rect(0, 0, slidder_now, this->height, 14, Color_lightblue);
	//Juli_fill(0, this->height / 2, slidder_now, this->height/2, Color_red);
	Juli_fill(slidder_now + 1, 0, this->width - slidder_now - 5, this->height + 1, Color_white);
	//Juli_fill_round_rect(slidder_now + 1, 0, this->width - slidder_now - 1, 10,this->height, Color_white);
}
// num pannel pressed event, return -1 if not captured
JUI volume_pannel_on_pressed(JUI this, int x, int y, TouchEvent mode)
{
	static int lastx = -1, x0 = 27;
	if (mode == PressedDown)
	{
		lastx = x;
		x0 = slidder_now;
		return NULL;
	}
	if (mode == PressedUp || mode == MoveOut)
	{
		lastx = -1;
		return NULL;
	}
	if (mode == Moving && lastx > 0)
	{
		//int dx = x - slidder_now;
		int dx = x - lastx;
		int xx = x0 + dx;
		if (xx < 30)
			xx = 30;
		if (xx >= this->width - 5)
			xx = this->width - 5;
		//Juli_fill(0, 0, xx, this->height, Color_lightblue);
		//Juli_fill(xx + 1, 0, this->width - xx - 1, this->height, Color_white);
		//if (dx < 0)
		//	dx = -dx;
		//if ((dx) > 0.1f * this->width)
		//	return -1;
		//dx = xx - slidder_now;

		// int f = slidder_now * this->width;
		/*if (dx > 0)
		{
			Juli_fill(slidder_now, 0, dx, this->height, Color_lightblue);
		}
		else
		{
			Juli_fill(slidder_now + dx, 0, -dx, this->height, Color_white);
		}*/
		//else
		//{
		//	Juli_fill(x, 0, -dx, this->height, Color_white);
		//}
		// f += 1;
		float bl = xx;
		bl = bl / this->width * 100;
#ifndef in_PC
		if (bl < 5)
			bl = 5;
		LCD_SSD_BackLightSet((u8)bl);
#endif
		slidder_now = xx;

		return this;
	}
	return NULL;
}
// x: 428-470, y:412:428, w: 70-28, h:28-12
void show_button(JUI this, ViewEvent e)
{
	string id = cast(this->id, string);
	if (*id == 'S')
	{
		Juli_fill(0, 0, this->width, this->height, Color_green);
		// LCD_Fill(428, 412, 470, 428, GREEN);
		//only_draw_text(1);
		//int n = strlen(id) * fontSize / 2;
		//Juli_put_text(this->width / 2 - n / 2, (this->height-fontSize)/2, id);
		////Juli_put_char(this->width / 2 - 8, 4, 'S');
		//only_draw_text(0);
	}
	else
	{
		Juli_fill(0, 0, this->width, this->height, Color_red);
		/*only_draw_text(1);
		Juli_put_char(this->width / 2 - 8, 4, 'X');
		only_draw_text(0);*/
	}

	only_draw_text(1);
	int n = strlen(id) * fontSize / 2;
	Juli_put_text(this->width / 2 - n / 2, (this->height - fontSize) / 2, id);
	//Juli_put_char(this->width / 2 - 8, 4, 'S');
	only_draw_text(0);

	// LCD_ShowChar(437 + 4, 412 + 1, 'S', 24, 0);
}
JUI button_on_pressed(JUI this, int x, int y, TouchEvent mode)
{
	if (/*mode == MoveIn ||*/ mode == PressedDown)
	{
		int n = strlen(this->id) * fontSize / 2;
		Juli_put_text(this->width / 2 - n / 2, (this->height - fontSize) / 2, this->id);

		//Juli_put_char(this->width / 2 - 8, 4, toUpper(cast(this->id, char)));
	}
	else if (mode == MoveOut)
	{
		return this;
		//show_button(this, OnTouchEvent);
	}
	else if (mode == PressedUp)
	{
		char *id = cast(this->id, char *);
		if (*id == 'X')
		{
			// *inputs-- = '\0';
			// *inputs = ' ';
			// inputs_delete();
			// stop_spi();
			//video_mode = 0;
			reload_activity();
		}
		else
		{
			JUI line = inputs_control_event("resume");
			if (line)
				juli_show_ui(line, ByPass);

			//video_mode = 1;
			// start_spi_rec(0);
		}
		return this;
		//show_button(this, OnTouchEvent);
		// return ('t');
	}
	return NULL;
}

void reload_activity()
{
	//LCD_Clear(Color_white);
	//LCD_DrawRectangle(4, 519, 480 - 4, 519 + 261);
	//LCD_DrawLine(inputs_pos_x + 12 * 2, inputs_pos_y + 24 + 2, 480 - 12, inputs_pos_y + 24 + 2);
	juli_show_up();
}
